angular.module('Measure')
    .controller('Measure.MeasureCtrl', ['$scope', '$state', '$http', '$ionicLoading', '$ionicPopup', 'Http', 'imanDialog', 'imanUsbPort',
        function($scope, $state, $http, $ionicLoading, $ionicPopup, Http, imanDialog, imanUsbPort) {
            $scope.data = {
                getQc: false,
                Barcode: ''
            };
            $scope.imgPath = {
                cycle: u9ImHd.getFullPath('/img/cycle.png', 'Measure'),
                weight: u9ImHd.getFullPath('/img/weight.png', 'Measure')
            };

            var toast;

            $scope.showToast = function(styleMes) {
                if (toast) {
                    toast.hide();
                }
                toast = imanDialog.toast({
                    message: styleMes,
                    position: 'center',
                    duration: false,
                    backdropClickToClose: false,
                    hardwareBackButtonClose: false
                });
            };

            $scope.showToast('请扫描单品信息');

            $scope.hideToast = function() {
                if (toast) {
                    toast.hide();
                    toast = null;
                }
            };

            $scope.$on('$ionicView.beforeEnter', function() {
                imanUsbPort.start();
            });

            $scope.$on('imanUsbPort.input', function(e, inputStr) {
                $scope.data.Barcode = inputStr;
                getQcInfo($scope.data.Barcode);
            });

            $scope.submit = function() {
                if (!$scope.data.qcStandard[0].NormValue || !$scope.data.qcStandard[1].NormValue) {
                    imanDialog.alert('卷制测量还未有数据', '信息不全');
                    return;
                }
                $ionicLoading.show();
                Http.post({
                    url: '/api/Wrapper/setWrapperMeasure',
                    data: {
                        Parameter: {
                            Barcode: $scope.data.Barcode,
                            TerminalUniqueID: '11:33:55:77',
                            Work: $scope.data.barcode.Work || 0,
                            WorkName: $scope.data.barcode.WorkName || '',
                            Users: $scope.data.Users,
                            QCRecords: $scope.data.qcStandard
                        }
                    }
                }).then(function() {
                    $ionicLoading.hide();
                    $scope.data.getQc = true;
                    imanDialog.alert('确认成功', '提交成功');
                }, function(err) {
                    $ionicLoading.hide();
                })['finally'](function() {
                    $ionicLoading.hide();
                });
            };

            function getQcInfo(Barcode) {
                $ionicLoading.show();
                Http.post({
                    url: '/api/Wrapper/getWrapperInfo',
                    data: {
                        Parameter: {
                            Barcode: Barcode,
                            TerminalUniqueID: '11:33:55:77' //todo
                        }
                    }
                }).then(function(info) {
                    $scope.data.getQc = true;
                    operateBarCode(info);
                })['finally'](function() {
                    $ionicLoading.hide();
                });
            }

            function operateBarCode(info) {
                if (info.basicInfo.MOCode) {
                    $scope.hideToast();
                }
                $scope.data.barcode = info.basicInfo;
                $scope.data.qcStandard = info.qcStandard;
                $scope.data.Users = info.basicInfo.Users;
            }

            $scope.$on('$ionicView.afterLeave', function() {
                imanUsbPort.stop();
                if (toast) {
                    toast.hide();
                    toast = null;
                }
            });

        }
    ]);
